package com.warthur.jfinal.demo.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.warthur.jfinal.demo.interceptor.Http;
import com.warthur.jfinal.demo.pojo.domain.User;
import com.warthur.jfinal.demo.pojo.param.UserBodyParam;
import com.warthur.jfinal.demo.service.UserService;
import com.warthur.jfinal.demo.validator.UserValidator;
import io.jboot.Jboot;
import io.jboot.db.model.Columns;
import io.jboot.support.redis.JbootRedis;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

/**
 * @author warthur
 * @date 2019/02/27
 */
@RequestMapping("/")
@Slf4j
public class HelloController extends BaseController {

    @Inject
    private UserService userService;

    @ActionKey("/hello")
    @Before(Http.GET.class)
    public void index(@Para("key") String key) {

        JbootRedis redis = Jboot.getRedis();
        redis.set("age", "100");
        String age = redis.get("age");

        renderText("Hello World!");
    }

    @ActionKey("/user")
    @Before(Http.GET.class)
    public void test() {

        Columns columns = Columns.create().add("user_id", "1");
        User user = userService.findById("1");

        renderJson(user);
    }

    @ActionKey("/user/create")
    @Before({Http.POST.class, UserValidator.class})
    public void user() {

        UserBodyParam body = getJsonBody(UserBodyParam.class);

        log.info(body.getUserId());

        Columns columns = Columns.create().add("user_id", "1");
        User user = userService.findById("1");

        renderJson(user);
    }
}
